﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ObviousCode.UI.Aspect.PluginLibrary.Interfaces;
using System.Collections;

namespace ObviousCode.UI.Aspect.PluginLibrary.Collections
{
    public class SurfaceObjectFactoryCollection : IEnumerable<ISurfaceObjectFactory>
    {
        List<ISurfaceObjectFactory> _factories;

        public SurfaceObjectFactoryCollection() : this(new List<ISurfaceObjectFactory>())
        {

        }

        public SurfaceObjectFactoryCollection(IEnumerable<ISurfaceObjectFactory> factories)
        {
            _factories = new List<ISurfaceObjectFactory>(factories);            
        }

        //public Surface

        public T GetFactory<T>() where T : ISurfaceObjectFactory
        {
            return (T) _factories.Where(f => f.GetType() == typeof(T)).FirstOrDefault();
        }
        
        public IEnumerator<ISurfaceObjectFactory> GetEnumerator()
        {
            foreach (ISurfaceObjectFactory factory in _factories)
            {
                yield return factory;
            }
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }        

        public ISurfaceObjectFactory GetFactory(string descriptorKey)
        {
            return _factories.Where(f => f.ContainsKey(descriptorKey)).FirstOrDefault();
        }
        
        public void Add(ISurfaceObjectFactory factory)
        {            
            _factories.Add(factory);
        }

        public void AddRange(IEnumerable<ISurfaceObjectFactory> factories)
        {
            _factories.AddRange(factories);
        }        
    }
}
